package ch.rheintalconnection.sysventory.technicalServices.wmi;

import ch.rheintalconnection.sysventory.dto.ComputerDto;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.EnumVariant;
import com.jacob.com.Variant;

public class ComputerScan {
	
	private ActiveXComponent axWMI;
	
	public static final String QUERY_COMPUTERSYSTEM = "SELECT * FROM Win32_ComputerSystem";
	
		public ComputerScan(ActiveXComponent axWMI) {
				this.axWMI = axWMI;				
		}
	
	
	public ComputerDto scan() {
		
		ComputerDto computerDto = null;
		
		Variant vCollection = axWMI.invoke("ExecQuery", new Variant(QUERY_COMPUTERSYSTEM));
		EnumVariant enumVariant = new EnumVariant(vCollection.toDispatch());
		Dispatch item = null;
		
		while (enumVariant.hasMoreElements()) {
			item = enumVariant.nextElement().toDispatch();
			
			String computerName = Dispatch.call(item, "Name").toString();
			String ram = Dispatch.call(item, "TotalPhysicalMemory").toString();
			String computerType = Dispatch.call(item, "PCSystemType").toString();
			
			computerDto = new ComputerDto(computerName, ram, computerType);
			return computerDto;
		}
		return null;
	}
}
